﻿using System;
using NAnt.Core;
using NAnt.Core.Attributes;
using NAnt.SVN.Tasks;
using NAnt.SVN.Util;
using NAnt.SVN.Tasks.Constants;

namespace NAnt.SVN.Attributes
{
    /// <summary>
    /// Validator to check if a proparty is a valid SVN property. 
    /// Based on the NAnt sourcecode for a Boolean validator
    /// $Id$
    /// </summary>
    public sealed class PropertyValidator : ValidatorAttribute
    {
        private SVNProperties propertiesobj = new SVNProperties();

        #region Public Instance Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyValidator" /> 
        /// class.
        /// </summary>
        public PropertyValidator()
        {
        }

        #endregion Public Instance Constructors

        #region Override implementation of ValidatorAttribute

        /// <summary>
        /// Checks if the specified value can be converted to a valid parameter
        /// </summary>
        /// <param name="value">The value to be checked.</param>
        /// <exception cref="ValidationException"><paramref name="value" /> cannot be converted to a <see cref="bool" />.</exception>
        public override void Validate(object value)
        {
            try
            {
                string stringvalue = Convert.ToString(value);
                if (!propertiesobj.IsStaticField(stringvalue))
                    throw new Exception(Strings.GetString("SVN0302"));
            }
            catch (Exception ex)
            {
                throw new ValidationException(string.Format(Strings.GetString("SVN0301"), value.ToString()), ex);
            }
        }

        #endregion Override implementation of ValidatorAttribute
    }
}
